package com.lt.lumentracks.controller;


import com.lt.lumentracks.constant.CommentConstant;
import com.lt.lumentracks.entity.po.Comment;
import com.lt.lumentracks.entity.vo.comment.CommentVO_ReplyToMe;
import com.lt.lumentracks.entity.vo.comment.CommentVO_liked;
import com.lt.lumentracks.entity.vo.comment.CommentVO_post;
import com.lt.lumentracks.entity.vo.common.PageVO;
import com.lt.lumentracks.entity.vo.common.Result;
import com.lt.lumentracks.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author swl
 * @since 2025-08-18
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService iCommentService;

    // 增
    // 新增一条评论
    @PostMapping("/add")
    public Result add(@RequestBody @Validated Comment comment) {
        return iCommentService.addComment(comment);
    }


    // 删
    // 删除一条评论
    @DeleteMapping("/del/{id}")
    public Result<String> del(@PathVariable Integer id) {
        iCommentService.removeCommentById(id);
        return Result.success("删除成功");
    }


    // 改
    // 修改该帖子的回复数量
    @PutMapping("/update")
    public Result<String> update(@RequestBody Comment comment, @RequestParam Boolean addOrMinus) {
        if(comment.getParentId() != 0 && comment.getLevel() != 1){
            return Result.error("参数错误，非顶级评论无法设置回复数量");
        }
        iCommentService.updateRepliesCount(comment, addOrMinus);
        return Result.success("修改成功");
    }


    // 查
    // 分页查询某个帖子下的评论
    @GetMapping("/get/getByPostId")
    public Result<PageVO<CommentVO_post>> getByPostId(@RequestParam Integer postId,
                                                      @RequestParam(defaultValue = CommentConstant.SORT_DEFAULT) String sortRule, // 排序规则
                                                      @RequestParam(defaultValue = "1") long current,
                                                      @RequestParam(defaultValue = "10") long size) {
        PageVO<CommentVO_post> pageVO = iCommentService.getByPostId(postId, sortRule, current, size);
        return Result.success(pageVO);
    }

    /** 查询某帖子下的评论的子评论
     * @param postId 帖子id
     * @param cid 父评论id
     * @param current 当前页码
     * @param size 每页数量
     * @return 子评论列表
     */
    @GetMapping("/get/getReplies")
    public Result<List<CommentVO_post>> getReplies(@RequestParam Integer postId,
                                                   @RequestParam Long cid,
                                                   @RequestParam(defaultValue = "1") long current,
                                                   @RequestParam(defaultValue = "5") long size) {
        List<CommentVO_post> vo = iCommentService.getReplies(postId, cid, current, size);
        return Result.success(vo);
    }

    /**
     * 查询谁回复了我的评论
     * @param current 当前页码
     * @param size 每页数量
     * @return 评论列表
     */
    @GetMapping("/getMy/getForMyRelies")
    public Result<PageVO<CommentVO_ReplyToMe>> getForMyRelies(@RequestParam(defaultValue = "1") long current,
                                                              @RequestParam(defaultValue = "10") long size) {
        return iCommentService.getForMyRelies(current, size);
    }


    // redis相关
    /** 点赞评论
     * @param cid 评论的id
     * @param userId_liked 被点赞（或取消点赞）的用户id
     * @param addOrDel true-点赞，false-取消点赞
     */
    @PostMapping("/like")
    public Result<String> addLikeRecord(@RequestParam Long cid,
                                        @RequestParam Integer userId_liked,
                                        @RequestParam Boolean addOrDel) {
        iCommentService.addLikeRecord(cid, userId_liked, addOrDel);
        return Result.success();
    }

    // 查询某些评论的点赞状态
    @PostMapping("/like-status")
    public Result<Set<Long>> isLiked(@RequestBody List<Long> cIds) {
        return iCommentService.isLiked(cIds);
    }

    // 查询我的评论--最近被点赞的
    @GetMapping("/getMy/recent-liked")
    public Result<PageVO<CommentVO_liked>> getMyRecentLikedComments(@RequestParam(defaultValue = "1") long current,
                                                                    @RequestParam(defaultValue = "10") long size) {
        return iCommentService.getMyRecentLikedComments(current, size);
    }

}
